// cloudfunctions/getSuggestions/index.js
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { keyword } = event;
  
  if (!keyword) return { success: true, data: [] };
  
  try {
    const res = await db.collection('sendgood')
      .where({
       productName: new RegExp(`^${keyword}`, 'i') // 匹配开头
      })
      .field({ productName: 1 })
      .limit(5)
      .get();
      
    return {
      success: true,
      data: res.data.map(item => item.productName)
    };
  } catch (error) {
    console.error('获取建议失败:', error);
    return {
      success: false,
      message: '获取建议失败'
    };
  }
};